package finedev.work.vennue;

import java.io.IOException;
import java.net.MalformedURLException;

import org.json.JSONObject;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.Button;

import com.facebook.android.DialogError;
import com.facebook.android.Facebook;
import com.facebook.android.Facebook.DialogListener;
import com.facebook.android.FacebookError;

public class IntroActivity extends Activity {
	
	private final String TAG = "Intro";
    private Activity mActivity;
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
    	super.onCreate(savedInstanceState);
    	setContentView(R.layout.activity_intro);
    	
    	mActivity = this;
    	
    	if ( BasicInfo.FacebookInstance == null ) {
    		BasicInfo.FacebookInstance = new Facebook( BasicInfo.APP_ID );
//    		Utility.mFacebook = BasicInfo.FacebookInstance;
    	}
    	
    	Button btn_login_fb = (Button) findViewById( R.id.login_facebook );
    	Button btn_login = (Button) findViewById( R.id.login );
//    	Button btn_join = (Button) findViewById( R.id.join );
    	
    	btn_login_fb.setOnClickListener( btnOnClickListener );
    	btn_login.setOnClickListener( btnOnClickListener );
//    	btn_join.setOnClickListener( btnOnClickListener );
    	
    	
    }
    
    private Button.OnClickListener btnOnClickListener = new Button.OnClickListener()
    {

		@Override
		public void onClick(View v) {
			// TODO Auto-generated method stub
			if ( v.getId() == R.id.login_facebook) {
				
				if ( BasicInfo.FacebookLogin == true ) {
					BasicInfo.FacebookInstance.setAccessToken( BasicInfo.FACEBOOK_ACCESS_TOKEN );
				} else {
					BasicInfo.FacebookInstance.authorize( mActivity, new String[] { "email", "read_stream", "publish_stream"},new DialogListener() {
						@Override
						public void onComplete(Bundle values) {
							try {
								String resultStr = BasicInfo.FacebookInstance.request("me");
								JSONObject obj = new JSONObject( resultStr );
								
								BasicInfo.FACEBOOK_NAME = obj.getString("name");

								BasicInfo.FacebookLogin = true;
								BasicInfo.FACEBOOK_ACCESS_TOKEN = BasicInfo.FacebookInstance.getAccessToken();
							} catch ( Exception ex) {
								ex.printStackTrace();
							}
							
							Log.d(TAG, "Authorization completed : " + BasicInfo.FACEBOOK_NAME);
							
							startActivity( new Intent( mActivity, JoinActivity.class) );
//							startActivity( new Intent( mActivity, MainTabActivity.class ) );
						}
	
						@Override
						public void onFacebookError(FacebookError error) {
						}
	
						@Override
						public void onError(DialogError e) {
						}
	
						@Override
						public void onCancel() {
						}
					});
				}
			} else if ( v.getId() == R.id.login ) {
				
				startActivity( new Intent( mActivity, LoginActivity.class) );
				
			}
//			else if ( v.getId() == R.id.join ) {
				
//				startActivity( new Intent( mActivity, JoinActivity.class) );
				
//			} 
			
		}
    	
    };

    @Override 
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        BasicInfo.FacebookInstance.authorizeCallback(requestCode, resultCode, data);
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
}